#include "definitions.h"

void heat_3d_uday_omp(double M[][2][Npadded][Npadded][Npadded], int pT, int pN) {
    int t1, t2, t3, t4, t5, t6, t7, t8;

    int lb, ub, lbp, ubp, lb2, ub2;
    register int lbv, ubv;
    
    /* Start of CLooG code */
    if ((pN >= 1) && (pT >= 2)) {
        for (t1=-1; t1<=floord(pT-2,8); t1++) {
            lbp=max(ceild(t1,2),ceild(16*t1-pT+3,16));
            ubp=min(floord(pT+pN-2,16),floord(8*t1+pN+7,16));
            // #pragma omp parallel for private(lbv,ubv)
            #pragma omp parallel for private(lbv,ubv,t2,t3,t4,t5,t6,t7,t8)

            for (t2=lbp; t2<=ubp; t2++) {
                for (t3=max(max(0,ceild(t1-1,2)),ceild(16*t2-pN-14,16)); t3<=min(min(floord(pT+pN-2,16),floord(8*t1+pN+15,16)),floord(16*t2+pN+14,16)); t3++) {
                    for (t4=max(max(max(0,ceild(t1-124,125)),ceild(16*t2-pN-998,1000)),ceild(16*t3-pN-998,1000)); t4<=min(min(min(min(floord(8*t1-8*t2+pN+7,500),floord(pT+pN-2,1000)),floord(8*t1+pN+15,1000)),floord(16*t2+pN+14,1000)),floord(16*t3+pN+14,1000)); t4++) {
                        for (t5=max(max(max(max(max(0,8*t1),16*t2-pN),16*t3-pN),1000*t4-pN),16*t1-16*t2+1); t5<=min(min(min(min(min(pT-2,8*t1+15),16*t2+14),16*t3+14),1000*t4+998),16*t1-16*t2+pN+15); t5++) {
                            if(t5%2==0) {
                                for (t6=max(max(16*t2,t5+1),-16*t1+16*t2+2*t5-15); t6<=min(min(16*t2+15,t5+pN),-16*t1+16*t2+2*t5); t6++) {
                                    for (t7=max(16*t3,t5+1); t7<=min(16*t3+15,t5+pN); t7++) {
                                        lbv=max(1000*t4,t5+1);
                                        ubv=min(1000*t4+999,t5+pN);
#pragma ivdep
#pragma vector always
                                        for (t8=lbv; t8<=ubv; t8++) {
                                            M[0][1][-t5+t6][-t5+t7][-t5+t8]=0.125*(M[0][0][-t5+t6+1][-t5+t7][-t5+t8]-2.0*M[0][0][-t5+t6][-t5+t7][-t5+t8]+M[0][0][-t5+t6-1][-t5+t7][-t5+t8])+0.125*(M[0][0][-t5+t6][-t5+t7+1][-t5+t8]-2.0*M[0][0][-t5+t6][-t5+t7][-t5+t8]+M[0][0][-t5+t6][-t5+t7-1][-t5+t8])+0.125*(M[0][0][-t5+t6][-t5+t7][-t5+t8-1]-2.0*M[0][0][-t5+t6][-t5+t7][-t5+t8]+M[0][0][-t5+t6][-t5+t7][-t5+t8+1])+M[0][0][-t5+t6][-t5+t7][-t5+t8];;
                                        }
                                    }
                                }
                            }
                            else {
                                for (t6=max(max(16*t2,t5+1),-16*t1+16*t2+2*t5-15); t6<=min(min(16*t2+15,t5+pN),-16*t1+16*t2+2*t5); t6++) {
                                    for (t7=max(16*t3,t5+1); t7<=min(16*t3+15,t5+pN); t7++) {
                                        lbv=max(1000*t4,t5+1);
                                        ubv=min(1000*t4+999,t5+pN);
#pragma ivdep
#pragma vector always
                                        for (t8=lbv; t8<=ubv; t8++) {
                                            M[0][0][-t5+t6][-t5+t7][-t5+t8]=0.125*(M[0][1][-t5+t6+1][-t5+t7][-t5+t8]-2.0*M[0][1][-t5+t6][-t5+t7][-t5+t8]+M[0][1][-t5+t6-1][-t5+t7][-t5+t8])+0.125*(M[0][1][-t5+t6][-t5+t7+1][-t5+t8]-2.0*M[0][1][-t5+t6][-t5+t7][-t5+t8]+M[0][1][-t5+t6][-t5+t7-1][-t5+t8])+0.125*(M[0][1][-t5+t6][-t5+t7][-t5+t8-1]-2.0*M[0][1][-t5+t6][-t5+t7][-t5+t8]+M[0][1][-t5+t6][-t5+t7][-t5+t8+1])+M[0][1][-t5+t6][-t5+t7][-t5+t8];;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    /* End of CLooG code */
}



void heat_3d_uday_no_skew(double M[][2][Npadded][Npadded][Npadded], int pT, int pN) {
  int t1, t2, t3, t4, t5, t6, t7, t8;

  int lb, ub, lbp, ubp, lb2, ub2;
  register int lbv, ubv;

  /* Start of CLooG code */
  if ((pN >= 1) && (pT >= 2)) {
    for (t1=ceild(-pN-15,16);t1<=floord(pT-3,16);t1++) {
      for (t2=max(t1,-t1-1);t2<=min(min(floord(-8*t1+pT-2,8),floord(8*t1+pN+7,8)),floord(pT+pN-2,16));t2++) {
        for (t3=max(max(0,ceild(t1+t2-1,2)),ceild(16*t2-pN-14,16));t3<=min(min(floord(pT+pN-2,16),floord(16*t2+pN+14,16)),floord(8*t1+8*t2+pN+15,16));t3++) {
          for (t4=max(max(max(0,ceild(t1+t2-124,125)),ceild(16*t2-pN-998,1000)),ceild(16*t3-pN-998,1000));t4<=min(min(min(min(floord(8*t1+pN+7,500),floord(pT+pN-2,1000)),floord(16*t2+pN+14,1000)),floord(16*t3+pN+14,1000)),floord(8*t1+8*t2+pN+15,1000));t4++) {
            for (t5=max(max(max(max(max(0,8*t1+8*t2),16*t1+1),16*t2-pN),16*t3-pN),1000*t4-pN);t5<=min(min(min(min(min(pT-2,16*t2+14),16*t3+14),1000*t4+998),8*t1+8*t2+15),16*t1+pN+15);t5++) {
              if (t5%2 == 0) {
                for (t6=max(max(16*t2,t5+1),-16*t1+2*t5-15);t6<=min(min(-16*t1+2*t5,16*t2+15),t5+pN);t6++) {
                  for (t7=max(16*t3,t5+1);t7<=min(16*t3+15,t5+pN);t7++) {
                    lbv=max(1000*t4,t5+1);
                    ubv=min(1000*t4+999,t5+pN);
#pragma ivdep
#pragma vector always
                    for (t8=lbv;t8<=ubv;t8++) {
                      M[0][1][-t5+t6][-t5+t7][-t5+t8]=0.125*(M[0][0][-t5+t6+1][-t5+t7][-t5+t8]-2.0*M[0][0][-t5+t6][-t5+t7][-t5+t8]+M[0][0][-t5+t6-1][-t5+t7][-t5+t8])+0.125*(M[0][0][-t5+t6][-t5+t7+1][-t5+t8]-2.0*M[0][0][-t5+t6][-t5+t7][-t5+t8]+M[0][0][-t5+t6][-t5+t7-1][-t5+t8])+0.125*(M[0][0][-t5+t6][-t5+t7][-t5+t8-1]-2.0*M[0][0][-t5+t6][-t5+t7][-t5+t8]+M[0][0][-t5+t6][-t5+t7][-t5+t8+1])+M[0][0][-t5+t6][-t5+t7][-t5+t8];;
                    }
                  }
                }
              }else{
                for (t6=max(max(16*t2,t5+1),-16*t1+2*t5-15);t6<=min(min(-16*t1+2*t5,16*t2+15),t5+pN);t6++) {
                  for (t7=max(16*t3,t5+1);t7<=min(16*t3+15,t5+pN);t7++) {
                    lbv=max(1000*t4,t5+1);
                    ubv=min(1000*t4+999,t5+pN);
#pragma ivdep
#pragma vector always
                    for (t8=lbv;t8<=ubv;t8++) {
                      M[0][0][-t5+t6][-t5+t7][-t5+t8]=0.125*(M[0][1][-t5+t6+1][-t5+t7][-t5+t8]-2.0*M[0][1][-t5+t6][-t5+t7][-t5+t8]+M[0][1][-t5+t6-1][-t5+t7][-t5+t8])+0.125*(M[0][1][-t5+t6][-t5+t7+1][-t5+t8]-2.0*M[0][1][-t5+t6][-t5+t7][-t5+t8]+M[0][1][-t5+t6][-t5+t7-1][-t5+t8])+0.125*(M[0][1][-t5+t6][-t5+t7][-t5+t8-1]-2.0*M[0][1][-t5+t6][-t5+t7][-t5+t8]+M[0][1][-t5+t6][-t5+t7][-t5+t8+1])+M[0][1][-t5+t6][-t5+t7][-t5+t8];;
                    }
                  }
                }


              }
            }
          }
        }
      }
    }
  }
  /* End of CLooG code */
}
